import type { IResData } from '@/api/indexTypes';
import { onPageScroll } from '@dcloudio/uni-app';
import { ref, unref } from 'vue';

interface Props {
  api?: any;
}

export interface ActionType {
  // 更新数据
  refresh: () => Promise<IResData>;
  addData: () => Promise<IResData>;
  setApi: (api: any) => void;
  closeAll: () => void;
}

export type RegisterFn = (formInstance: ActionType) => void;

export type UseReturnType = [RegisterFn, ActionType];

export function useConList(props?: Props): UseReturnType {
  const listRef = ref<Nullable<ActionType>>(null);

  function register(instance: ActionType) {
    listRef.value = instance;
    props && props.api && instance.setApi(props.api);
  }

  function getList() {
    const form = unref(listRef);
    return form as ActionType;
  }

  const methods = {
    refresh: () => getList()?.refresh(),
    addData: () => getList()?.addData(),
    setApi: (api: any) => getList().setApi(api),
    closeAll: () => getList().closeAll(),
  };


  onPageScroll(() => {});

  return [register, methods];
}
